﻿/*
#-------------------------------------------------------------------------------
# Name:        LicensePlate - Holds information and methods for LicensePlate objects
#
# Author:      Kasper van Voorst
#
# Created:     03-04-2012
#-------------------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LibraryCS
{
    public class LicensePlate
    {
        public Point topleft;
        public Point bottomleft;
        public Point topright;
        public Point bottomright;

        public int topwidth;
        public int leftheight;
        public int bottomwidth;
        public int rightheigth;



        public string text;
        public string filename;

        public LicensePlate()
        {
        }

        public LicensePlate(Point tl, Point bl, Point tr, Point br, string txt, string fname)
        {
            topleft = tl;
            bottomleft = bl;
            topright = tr;
            bottomright = br;
            text = txt;
            filename = fname;

            topwidth = topright.X - topleft.X;
            leftheight = bottomleft.Y - topleft.Y;
            bottomwidth = bottomright.X - bottomleft.X;
            rightheigth = bottomright.Y - topright.Y;
        }

        public string toString() {
            string s = "topleft: " + topleft + ", topright: " + topright + ", bottomleft: " + bottomleft + ", bottomright: " + bottomright + ", text: " + text + ", filename: " + filename;
            return s;
        }

        public double comparePoints(Point p1, Point p2 ) {
            double xResult;
            double yResult;
            if (p1.X > p2.X) {
                xResult = ((double)(p1.X - p2.X) / (double)p1.X) * 100;
            } else {
                xResult = ((double)(p2.X - p1.X) / (double)p2.X) * 100;
            }
            if (p1.Y > p2.Y) {
                yResult = ((double)(p1.Y - p2.Y) / (double)p1.Y) * 100;
            } else {
                yResult = ((double)(p2.Y - p1.Y) / (double)p2.Y) * 100;
            }

            return 100 - ((xResult + yResult) / 2);
        }

        public double comparePointsX(int newpointx, int originalpointx, int comparison)
        {
            double difx = (double)Math.Abs(newpointx - originalpointx);
            double xResult = (difx / (double)comparison) * 100;

            return 100 - xResult;
        }

        public double comparePointsY(int newpointy, int originalpointy, int comparison)
        {
            double dify = (double)Math.Abs(newpointy - originalpointy);
            double yResult = (dify / (double)comparison) * 100;

            return 100 - yResult;
            
        }

        public double compareLocation(LicensePlate lp) {
            double newres = (comparePointsX(lp.topleft.X, topleft.X, topwidth) + comparePointsX(lp.topright.X, topright.X, topwidth) +
                             comparePointsX(lp.bottomleft.X, bottomleft.X, bottomwidth) + comparePointsX(lp.bottomright.X, bottomright.X, bottomwidth) +
                             comparePointsY(lp.topleft.Y, topleft.Y, leftheight) + comparePointsY(lp.topright.Y, topright.Y, rightheigth) +
                             comparePointsY(lp.bottomleft.Y, bottomleft.Y, leftheight) + comparePointsY(lp.bottomright.Y, bottomright.Y, rightheigth)) / 8;

            return newres;
        }

        public override string ToString()
        {
            String str = "Image: \"" + filename + "\", Location Numberplate:\n";
            str += "Top Left: (" + topleft.X + ", " + topleft.Y + ") , Top Right (" + topright.X + ", " + topright.Y + ")\n";
            str += "Bottom Left: (" + bottomleft.X + ", " + bottomleft.Y + ") , Bottom Right: (" + bottomright.X + ", " + bottomright.Y + ")\n";
            str += "License number found: " + text;

            return str;
        }
        public int compareText(LicensePlate lp) {
            string s = lp.text;
            int counter = 0;
            int index = 0;

            for (index = 0; index < s.Length; index++)
            {
                if (lp.text[index] == text[index])
                {
                    counter++;
                }
            }
            return counter;
        }
    }
}
